//
//  WSGNavigationController.m
//  oneDollar
//
//  Created by Laki on 16/6/15.
//  Copyright © 2016年 com.Laki.corder. All rights reserved.
//

#import "WSGNavigationController.h"

@interface WSGNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation WSGNavigationController

#pragma mark --------------------
#pragma mark 更改导航栏内容
/*
 * 更改导航栏
 */
+ (void)load{
    // 获取当前类的所有导航栏
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    // 改变导航栏的背景图片
    [bar setBackgroundImage:[UIImage imageNamed:@"Circle_MyCircleHeader"] forBarMetrics:UIBarMetricsDefault];
    
    // 创建一个可变字典,用于修改导航栏的各项属性
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    // 修改导航栏的字体和文字大小
    attributes[NSFontAttributeName] = [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:18.0];
    
    // 修改导航栏的文字颜色
    attributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0];
    
    // 设置
    [bar setTitleTextAttributes:attributes];
}

- (void)viewDidLoad{
    // 禁止系统自带的手势,两个手势冲突
    self.interactivePopGestureRecognizer.enabled = NO;
    
    // 创建一个拖动手势,调用系统的代理方法和系统的返回方法
    UIPanGestureRecognizer *navPan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    
    // 设置代理
    navPan.delegate = self;
    
    // 添加手势
    [self.view addGestureRecognizer:navPan];
}

#pragma mark --------------------
#pragma mark UIGestureRecognizerDelegate
// 是否触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    // 判断是否在根控制器下,如果是根控制器则禁止触发滑动返回功能
    return self.childViewControllers.count > 1;
}

#pragma mark --------------------
#pragma mark push跳转控制器时调用
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    // 判断是否是根控制器,如果是根控制器没必要在导航条上添加返回按钮
    if (self.childViewControllers.count > 0) {
        
        // 创建一个自定义按钮
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        // 给按钮设置图片
        [backBtn setImage:[UIImage imageNamed:@"NavChevron"] forState:UIControlStateNormal];
        
        // 让按钮根据内容自定义大小
        [backBtn sizeToFit];
        
        // 设置图片偏移
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        
        // 监听按钮的点击事件
        [backBtn addTarget:self action:@selector(backController) forControlEvents:UIControlEventTouchUpInside];
        
        // 添加按钮到导航栏左侧
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    }
    // 调用父类的跳转方法
    [super pushViewController:viewController animated:animated];
}

/*
 * 返回上一个控制器
 */
- (void)backController{
    [self popViewControllerAnimated:YES];
}

/*
 * 更改状态栏颜色
 */
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
